<?php

/**
 * This file is part of the Pagizer package.
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 * @copyright   Copyright (c) 2010 Advisa (http://www.advisa.fr)
 * @author      Pagizer Core Team <team@pagizer.org>
 * @package     pagizer
 * @subpackage  medias
 */

function smarty_function_socialShare($params, &$smarty)
{ 
	$xml = f_xml_Xpath::getInstance(MODULES_DIR.DS.'website'.DS.'config'.DS.'socialShare.xml');

    $networks = $xml->getElementsFromXpath('network');

    $url = !isset($params['url']) ? 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'] : $params['url'];
    $title = !isset($params['title']) ? urlencode(f_core_Context::getInstance()->getContext('currentPage')->getMetaTitle()) : $params['title'];
    $text = !isset($params['text']) ? '' : $params['text'];

    if(count($networks) > 0)
    {
        foreach($networks as $network)
        {
            /* @var $network f_xml_XmlElement */
            $link = $network->getContent();
            $link = str_replace('{permalink}', $url, $link);
            $link = str_replace('{title}', $title, $link);
            $link = str_replace('{text}', $text, $link);
            echo '<a href="'.$link.'" class="share-'.$network->getAttributeValue('label').'" target="_blank" ></a>';
        }
    }
}